package com.tangkun.environment.network;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;

import com.tangkun.environment.activity.LoginActivity;
import com.tangkun.environment.entity.base.BaseModel;
import com.tangkun.environment.entity.base.CommonReturn;
import com.tangkun.environment.utils.LogUtils;
import com.tangkun.environment.utils.NetworkUtils;
import com.tangkun.environment.utils.ProgressDialogUtil;

import rx.Subscriber;

/**
 * Created Time: 2017/3/8.
 * <p>
 * Author:tangkun
 * <p>
 * 功能：请求数据返统一处理
 */
public abstract class MySubscribe<T extends BaseModel> extends Subscriber<BaseModel> {
    private Context mContext;
    private boolean showLoading;


    /**
     * @param context
     * @param showLoading 是否显示加载中
     */
    public MySubscribe(Context context, boolean showLoading) {
        this.mContext = context;
        this.showLoading = showLoading;

    }

    @Override
    public void onCompleted() {
        if (showLoading) {
            ProgressDialogUtil.dismiss();
        }
    }

    @Override
    public void onError(Throwable e) {
        e.printStackTrace();
        if (mContext != null) {
            if (!NetworkUtils.isConnected(mContext)) {
                onMyError("网络不可用");
            } else {
                LogUtils.e(e.toString());
                onMyError("" + e.toString());
            }
        }
        LogUtils.e(e.toString() + "");
        if (showLoading) {
            ProgressDialogUtil.dismiss();
        }

    }

    @Override
    public void onNext(BaseModel baseModel) {
        if (baseModel != null) {
            if (baseModel.code == GlobalFlag.returnFlag) {
                success((T) baseModel);
                //显示服务器空的通用Toast
//                Toast mToast = Toast.makeText(mContext, (String) baseModel.description, Toast.LENGTH_SHORT);
//                mToast.setGravity(Gravity.CENTER, 0, 0);//居中显示
//                mToast.show();
//            onErrorWithFlag(baseModel.ret, baseModel.message);
            } else if (baseModel.code == GlobalFlag.returnFlagUnLogin) {//未登录跳转登录
                if (baseModel.description != null && baseModel.description instanceof String && !TextUtils.isEmpty((String) baseModel.description)) {
                    onMyError((String) baseModel.description);
                } else {
                    onMyError("");
                }
                if (baseModel.description != null && baseModel.description instanceof CommonReturn) {
                    //处理description是一个对象的情况
                }
                Intent intent = new Intent(mContext, LoginActivity.class);
                mContext.startActivity(intent);
            } else {
                if (baseModel.description != null && baseModel.description instanceof String && !TextUtils.isEmpty((String) baseModel.description)) {
                    onMyError((String) baseModel.description);
                } else {
                    onMyError("");
                }
                if (baseModel.description != null && baseModel.description instanceof CommonReturn) {
                    //处理description是一个对象的情况
                }
//            onErrorWithFlag(baseModel.ret, baseModel.message);
            }
//        } else if (GlobalFlag.returnNoThor.equals(baseModel.ret)) {
//            onMyError(baseModel.message);
//            onErrorWithFlag(baseModel.ret, baseModel.message);
//        } else if (GlobalFlag.returnNoToken.equals(baseModel.ret)) {
//            onMyError(baseModel.message);
//            onErrorWithFlag(baseModel.ret, baseModel.message);
//        } else if (GlobalFlag.returnTokenFail.equals(baseModel.ret)) {
//            onMyError("登录已过期，请重新登录");
//            onErrorWithFlag(baseModel.ret, baseModel.message);
//        } else if (GlobalFlag.returnTokenOtherFail.equals(baseModel.ret)) {
//            onMyError(baseModel.message + ",请重新登录");
//            onErrorWithFlag(baseModel.ret, baseModel.message);
//        } else {
//            onMyError(baseModel.message);
//            onErrorWithFlag(baseModel.ret, baseModel.message);
//        }
        }
    }

    protected abstract void success(T t);

    protected abstract void onMyError(String errorMessage);

//    protected void onErrorWithFlag(String flag, String errorMessage) {
//
//    }


}
